package pack1022;
// StringBuilder
public class DemoB {

    // 如何产生对象
    private static void a(){
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder("abc");
        System.out.println(s1);
        System.out.println(s2);
    }
    // 常见方法
    private static void b(){
        StringBuilder s = new StringBuilder();
        // 追加内容
        s.append("aa");
        s.append(123);
        s.append(true);
        // 插入
        s.insert(1,"插入的内容");
        System.out.println(s);
        // 删除
        s.deleteCharAt(3); // 删除一个字符
        System.out.println(s);
        s.delete(1,4); // 删除区间内容 [1,4)
        System.out.println(s);
        s.reverse(); // 反转字符串
        System.out.println(s);
    }
    // 注意事项
    private static void c(){
        // 可变字符串
        StringBuilder s1 = new StringBuilder();
        // 不可变字符串
        String s2 = "";
        s1.append("abc");
        s2.concat("abc");
        System.out.println(s1);
        System.out.println(s2);
    }

    // StringBuilder和String之间类型转换
    private static void d(){
        String s1 = "abc";
        // String-->StringBuilder
        StringBuilder s2 = new StringBuilder(s1);
        // StringBuilder-->String
        String s3 = s2.toString();
        System.out.println(s2);
        System.out.println(s3);
    }
    public static void main(String[] args) {
        d();
    }
}